// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        cellList: [],
        cellPrefab: [cc.Prefab],
        cellGroup: {
            type: cc.Node,
            default: null
        },
        currentDieCell: [cc.Node],
        cellWidth: 70,
        canMove: true, //演化期间不能move
        endMove: true, //表示完成了上一个滑动
        cellID: 1,
        currentMusic: 1,
        prefabBoom: {
            type: cc.Prefab,
            default: null
        }

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        cc.log("game加载");
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchFunction, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.moveFunction, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);

        this.init();
    },

    start() {

    },
    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchFunction, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.moveFunction, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
    },
    touchFunction(event) {
        this.endMove = false;
        // cc.log(event);

        // let touchPos = event.getLocation();
        // cc.log(touchPos.x - 5);
        // cc.log(touchPos.y - 260);

    },
    moveFunction(event) {
        if (this.canMove && this.endMove == false) {
            let startTouchPos = event.getStartLocation();
            let herePos = event.getLocation();
            let absX = Math.abs(startTouchPos.x - herePos.x);
            let absY = Math.abs(startTouchPos.y - herePos.y);
            if (absX >= (this.cellWidth) || absY >= (this.cellWidth)) {
                //cc.log("移动成功");
                //移动成功之后给演变方法传入两个
                this.canMove = false;
                this.endMove = true;
                let startPos = cc.v2(startTouchPos.x - 5, startTouchPos.y - 5);
                let endPos = cc.v2(herePos.x - 5, herePos.y - 5);
                this.cellChange(startPos, endPos);
            }
        }
    },

    touchEnd() {
        //this.endMove = true;
    },

    //生成初始的cell 必定不能炸的 总公共9*9 81个  ，还要检查会不会无解的情况
    init() {


        //i=>y  j=>x 
        for (let i = 0; i < 9; i++) {
            this.cellList[i] = new Array();
            for (let j = 0; j < 9; j++) {

                let index = Math.floor((Math.random() * 3.99999999));
                if (i > 1 || j > 1) {

                    let lsList = [0, 1, 2, 3];
                    if (j > 1) {
                        let node1 = this.cellList[i][j - 2];
                        let node2 = this.cellList[i][j - 1];
                        if (node1.prefabType == node2.prefabType && node2.prefabType == index) {
                            lsList.splice(lsList.indexOf(index), 1);
                            if (index > 2) {
                                index--;
                            } else {
                                index++;
                            }
                        }
                    }

                    if (i > 1) {
                        let node1 = this.cellList[i - 2][j];
                        let node2 = this.cellList[i - 1][j];
                        if (node1.prefabType == node2.prefabType && node2.prefabType == index) {
                            lsList.splice(lsList.indexOf(index), 1);
                            index = lsList[0];

                            // if (index > 2) {
                            //     index--;
                            // } else {
                            //     index++;
                            // }
                        }
                    }


                }
                let node = cc.instantiate(this.cellPrefab[index]);
                node.x = j * 70;
                node.y = i * 70;
                node.prefabType = index;
                node.cellID = this.cellID++;
                node.cellState = 1; //1 活着的cell

                this.cellGroup.addChild(node);
                this.cellList[i][j] = node;
            }
        }
        //cc.log(this.cellList);
        cc.log(this.preCheckCell());
    },
    //演变方法，从两个pos转换两个cell的位置，需要action完成之后
    cellChange(startPos, endPos) {
        this.currentMusic = 1;
        // cc.log(startPos);
        // cc.log(endPos);
        let that = this;
        let startCell = this.cellList[Math.floor(startPos.y / this.cellWidth)][Math.floor(startPos.x / this.cellWidth)];
        let endCell = this.cellList[Math.floor(endPos.y / this.cellWidth)][Math.floor(endPos.x / this.cellWidth)];
        let startAction = cc.moveTo(0.2, cc.v2(endCell.x, endCell.y));
        //cc.log(startAction);

        let endAction = cc.moveTo(0.2, cc.v2(startCell.x, startCell.y));

        let callBackAction = cc.callFunc(function() {
            //cc.log("动作回调");
            //
            let dieCells = that.checkCell();
            //cc.log(dieCells);
            //爆炸是先消失然后爆炸还是爆炸后消失
            //消失 爆炸动画  ，补充，移动cell,
            //先消失 ，创建爆炸动画，动画播放完毕之后，删除，补充，移动cell
            //this.currentDieCell[]

            if (dieCells.length == 0) {
                //没有可消除的，返回原点
                startAction = cc.moveTo(0.2, cc.v2(endCell.x, endCell.y));
                endAction = cc.moveTo(0.2, cc.v2(startCell.x, startCell.y));
                startCell.runAction(startAction);
                endCell.runAction(endAction);
                that.cellList[Math.floor(startPos.y / that.cellWidth)][Math.floor(startPos.x / that.cellWidth)] = startCell;
                that.cellList[Math.floor(endPos.y / that.cellWidth)][Math.floor(endPos.x / that.cellWidth)] = endCell;
                that.canMove = true;
            } else {

                setTimeout(function() {
                    //新的
                    //
                    that.fillCellGroup();
                }, 200);
            }
        }, endCell);


        this.cellList[Math.floor(endPos.y / this.cellWidth)][Math.floor(endPos.x / this.cellWidth)] = startCell;
        // this.cellList[Math.floor(endPos.y / this.cellWidth)][Math.floor(endPos.x / this.cellWidth)].i = Math.floor(endPos.y / this.cellWidth);
        // this.cellList[Math.floor(endPos.y / this.cellWidth)][Math.floor(endPos.x / this.cellWidth)].j = Math.floor(endPos.x / this.cellWidth);

        this.cellList[Math.floor(startPos.y / this.cellWidth)][Math.floor(startPos.x / this.cellWidth)] = endCell;
        // this.cellList[Math.floor(startPos.y / this.cellWidth)][Math.floor(startPos.x / this.cellWidth)].i = Math.floor(startPos.y / this.cellWidth);
        // this.cellList[Math.floor(startPos.y / this.cellWidth)][Math.floor(startPos.x / this.cellWidth)].j = Math.floor(startPos.x / this.cellWidth);

        startCell.runAction(startAction);
        endCell.runAction(cc.sequence(endAction, callBackAction));
        //完成转换，进行第一次判断


    },



    //预检查是否无解
    preCheckCell() {

        let checkState = false;
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {

                //横向检查条件1 ,>0，从位置1开始
                if (j > 0) {
                    //横向检查条件2 横向上一个的type一样
                    if (this.cellList[i][j - 1].prefabType == this.cellList[i][j].prefabType) {
                        //检查六个位置的type,如果位置的i值和j值可行则进行检查
                        //横向情况1
                        let hereI = i;
                        let hereJ = j - 3;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //横向情况2
                        hereI = i + 1;
                        hereJ = j - 2;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //横向情况3
                        hereI = i - 1;
                        hereJ = j - 2;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //横向情况4
                        hereI = i;
                        hereJ = j + 2;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //横向情况5
                        hereI = i + 1;
                        hereJ = j + 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //横向情况6
                        hereI = i - 1;
                        hereJ = j + 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }

                    }


                }


                //竖向检查 条件1
                if (i > 0) {
                    //竖向检查条件2 竖向上一个的type一样
                    if (this.cellList[i - 1][j].prefabType == this.cellList[i][j].prefabType) {
                        //检查六个位置的type,如果位置的i值和j值可行则进行检查
                        //竖向情况1
                        let hereI = i - 3;
                        let hereJ = j;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //竖向情况2
                        hereI = i - 2;
                        hereJ = j + 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //竖向情况3
                        hereI = i - 2;
                        hereJ = j - 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //竖向情况4
                        hereI = i + 2;
                        hereJ = j;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //竖向情况5
                        hereI = i + 1;
                        hereJ = j + 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                        //竖向情况6
                        hereI = i + 1;
                        hereJ = j - 1;
                        if (hereI >= 0 && hereI < 9 && hereJ >= 0 && hereJ < 9) {
                            if (this.cellList[hereI][hereJ].prefabType == this.cellList[i][j].prefabType) {
                                checkState = true;
                                break;
                            }
                        }
                    }

                }


            }

            if (checkState) {
                break;
            }
        }

        return checkState;
    },

    //检查是否有可以消除的解
    checkCell() {

        let dieCellID = [];
        let that = this;
        this.currentDieCell = [];
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                //左
                let leftJ = j - 1;
                //右
                let rightJ = j + 1;
                if (leftJ >= 0 && rightJ < 9) {
                    //判断左中右情况
                    if (this.cellList[i][leftJ].prefabType == this.cellList[i][j].prefabType && this.cellList[i][j].prefabType == this.cellList[i][rightJ].prefabType) {
                        //命中情况检查三个是否加入消除队列
                        if (!dieCellID.includes(this.cellList[i][leftJ].cellID)) {
                            dieCellID.push(this.cellList[i][leftJ].cellID);
                            this.currentDieCell.push(this.cellList[i][leftJ]);
                        }
                        if (!dieCellID.includes(this.cellList[i][j].cellID)) {
                            dieCellID.push(this.cellList[i][j].cellID);
                            this.currentDieCell.push(this.cellList[i][j]);
                        }
                        if (!dieCellID.includes(this.cellList[i][rightJ].cellID)) {
                            dieCellID.push(this.cellList[i][rightJ].cellID);
                            this.currentDieCell.push(this.cellList[i][rightJ]);
                        }

                    }
                }
                //上
                let topI = i + 1;
                //下
                let bottomI = i - 1;
                if (topI < 9 && bottomI >= 0) {
                    //判断上中下情况
                    if (this.cellList[bottomI][j].prefabType == this.cellList[i][j].prefabType && this.cellList[i][j].prefabType == this.cellList[topI][j].prefabType) {
                        //命中情况检查三个是否加入消除队列命中情况检查三个是否加入消除队列
                        if (!dieCellID.includes(this.cellList[bottomI][j].cellID)) {
                            dieCellID.push(this.cellList[bottomI][j].cellID);
                            this.currentDieCell.push(this.cellList[bottomI][j]);
                        }
                        if (!dieCellID.includes(this.cellList[i][j].cellID)) {
                            dieCellID.push(this.cellList[i][j].cellID);
                            this.currentDieCell.push(this.cellList[i][j]);
                        }
                        if (!dieCellID.includes(this.cellList[topI][j].cellID)) {
                            dieCellID.push(this.cellList[topI][j].cellID);
                            this.currentDieCell.push(this.cellList[topI][j]);
                        }
                    }
                }



            }
        }


        return dieCellID;
    },
    //补充缺了的cell
    fillCellGroup() {
        //分两步，移动 补充
        //移除格子的操作
        let that = this;
        cc.find("eliminate" + that.currentMusic).getComponent(cc.AudioSource).play();
        if (that.currentMusic < 8) {
            that.currentMusic++;
        }
        that.currentDieCell.map(function(one, index) {
            //cc.log(one);
            one.active = false;
            let boom = cc.instantiate(that.prefabBoom);
            boom.x = one.x + that.cellWidth / 2;
            boom.y = one.y + that.cellWidth / 2;
            that.cellGroup.addChild(boom);
            one.cellState = 0;
            one.removeFromParent();
        });

        //检查移动需要所有格子检查
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                let oriCell = that.cellList[i][j];
                let currentI = i;
                while (currentI < 8) {
                    currentI = currentI + 1;
                    //cc.log(that.cellList[currentI][j]);
                    if (that.cellList[currentI][j].cellState == 1 && oriCell.cellState == 0) {
                        //cc.log("往下掉");
                        let action = cc.moveTo(0.15, cc.v2(j * 70, i * 70));
                        //let action = cc.moveTo(0.2, cc.v2(oriCell.x, oriCell.y));
                        //let action = cc.moveBy(0.2, cc.v2(0, -70));
                        //已经删除的格子网上诺，方便上面的格子定位掉下来的位置

                        // oriCell.x = that.cellList[currentI][j].x;
                        // oriCell.y = that.cellList[currentI][j].y;

                        this.cellList[currentI][j].runAction(action);
                        let zhongjian = null;
                        zhongjian = that.cellList[currentI][j];
                        that.cellList[currentI][j] = that.cellList[i][j];
                        that.cellList[i][j] = zhongjian;
                        currentI = 10;
                    }
                }
            }
        }

        //补充给所有的空格
        //i=>y  j=>x 
        setTimeout(() => {
            for (let i = 0; i < 9; i++) {
                for (let j = 0; j < 9; j++) {
                    //所有cell必须对齐
                    that.cellList[i][j].x = j * that.cellWidth;
                    that.cellList[i][j].y = i * that.cellWidth;
                    let index = Math.floor((Math.random() * 3.99999999));
                    if (that.cellList[i][j].cellState == 0) {
                        let node = cc.instantiate(that.cellPrefab[index]);
                        node.x = j * 70;
                        node.y = i * 70;
                        node.prefabType = index;
                        node.cellID = that.cellID++;
                        node.cellState = 1; //1 活着的cell
                        that.cellGroup.addChild(node);
                        that.cellList[i][j] = node;
                    }
                }
            }

            let r = that.checkCell();
            if (r.length > 0) {
                setTimeout(() => {
                    that.fillCellGroup();
                }, 500);
            } else {
                // for (let i = 0; i < 9; i++) {
                //     for (let j = 0; j < 9; j++) {
                //         //所有cell必须对齐
                //         that.cellList[i][j].x = j * that.cellWidth;
                //         that.cellList[i][j].y = i * that.cellWidth;
                //     }
                // }
                that.canMove = true;
            }


        }, 200);
    },
    // update (dt) {},
});